package com.abe.mvp.view.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.abe.app.RelayoutTool;
import com.abe.clickeffect.OnClickEffectTouchListener;
import com.abe.mvp.model.bean.MenuBean;
import com.abe.mvp.view.activity.LoginActivity;
import com.abe.mvp.view.activity.SettingActivity;
import com.abe.strengthtrainer.R;
import com.squareup.picasso.Picasso;

@SuppressLint({ "InflateParams", "ClickableViewAccessibility" })
public class SettingMenuAdapter extends BaseAdapter {

	public static final int TYPE_SETTINGMENU = 0x001;
	public static final int TYPE_LOGINTYPE = 0x002;
	public static final int TYPE_LIST = 0x003;

	private List<MenuBean> data;
	private LayoutInflater layoutInflater;
	private float scale;
	private Context context;
	private int type;

	public SettingMenuAdapter(Context context, List<MenuBean> data, float scale) {
		this.data = data;
		this.scale = scale;
		this.context = context;
		this.layoutInflater = LayoutInflater.from(context);
	}

	public SettingMenuAdapter(Context context, List<MenuBean> data,
			float scale, int type) {
		this.data = data;
		this.type = type;
		this.scale = scale;
		this.context = context;
		this.layoutInflater = LayoutInflater.from(context);
	}

	public void setListData(List<MenuBean> data) {
		this.data = data;
		this.notifyDataSetChanged();
	}

	public final class ViewHolder {
		public LinearLayout layout;
		public TextView name;
		public ImageView image;
	}

	public final class ViewHolder2 {
		public LinearLayout layout;
		public TextView name;
		public ImageView image, select;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	/**
	 * 获得某一位置的数
	 */
	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	/**
	 * 获得唯一标识
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	private View initLoginMenu(final int position, View convertView,
			ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			// 获得组件，实例化组件
			convertView = layoutInflater.inflate(R.layout.item_grid_login_menu,
					null);
			RelayoutTool.relayoutViewHierarchy(convertView, scale);
			holder.layout = (LinearLayout) convertView
					.findViewById(R.id.item_menu_layout);
			holder.name = (TextView) convertView
					.findViewById(R.id.item_menu_name);
			holder.image = (ImageView) convertView
					.findViewById(R.id.item_menu_image);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		MenuBean bean = data.get(position);
		holder.layout.setOnTouchListener(new OnClickEffectTouchListener());
		holder.name.setText(bean.getName());
		holder.layout.setBackgroundResource(bean.getDrawable());
		Picasso.with(context).load(bean.getImage()).into(holder.image);
		return convertView;
	}

	private View initSettingMenu(final int position, View convertView,
			ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			// 获得组件，实例化组件
			convertView = layoutInflater.inflate(
					R.layout.item_list_setting_menu, null);
			RelayoutTool.relayoutViewHierarchy(convertView, scale);
			holder.layout = (LinearLayout) convertView
					.findViewById(R.id.item_menu_layout);
			holder.name = (TextView) convertView
					.findViewById(R.id.item_menu_name);
			holder.image = (ImageView) convertView
					.findViewById(R.id.item_menu_image);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		MenuBean bean = data.get(position);
		holder.name.setText(bean.getName());
		holder.name.setTextColor(context.getResources().getColor(
				bean.getTextcolor()));
		holder.layout.setBackgroundColor(context.getResources().getColor(
				bean.getDrawable()));
		Picasso.with(context).load(bean.getImage()).into(holder.image);
		return convertView;
	}

	private View initLoginTypeMenu(final int position, View convertView,
			ViewGroup parent) {
		ViewHolder2 holder = null;
		if (convertView == null) {
			holder = new ViewHolder2();
			// 获得组件，实例化组件
			convertView = layoutInflater.inflate(
					R.layout.item_list_setting_login_type, null);
			RelayoutTool.relayoutViewHierarchy(convertView, scale);
			holder.layout = (LinearLayout) convertView
					.findViewById(R.id.item_menu_layout);
			holder.name = (TextView) convertView
					.findViewById(R.id.item_menu_name);
			holder.image = (ImageView) convertView
					.findViewById(R.id.item_menu_image);
			holder.select = (ImageView) convertView
					.findViewById(R.id.item_image_select);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder2) convertView.getTag();
		}

		MenuBean bean = data.get(position);
		holder.name.setText(bean.getName());
		holder.layout.setOnTouchListener(new OnClickEffectTouchListener());
		holder.layout.setBackgroundResource(bean.getDrawable());
		Picasso.with(context).load(bean.getImage()).into(holder.image);
		if (bean.isSelected()) {
			holder.select.setBackgroundResource(R.drawable.item_select);
		} else {
			holder.select.setBackgroundColor(context.getResources().getColor(
					R.color.transparent));
		}
		return convertView;
	}

	private View initSettingListView(int position, View convertView,
			ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			// 获得组件，实例化组件
			convertView = layoutInflater.inflate(R.layout.item_list_device,
					null);
			RelayoutTool.relayoutViewHierarchy(convertView, scale);
			holder.layout = (LinearLayout) convertView
					.findViewById(R.id.item_layout);
			holder.name = (TextView) convertView.findViewById(R.id.item_name);
			holder.image = (ImageView) convertView
					.findViewById(R.id.item_select);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		MenuBean bean = data.get(position);
		holder.layout.setOnTouchListener(new OnClickEffectTouchListener());
		holder.name.setText(bean.getNameStr());
		if (bean.isSelected()) {
			holder.image.setBackgroundResource(R.drawable.icon_item_select);
		} else {
			holder.image.setBackgroundColor(context.getResources().getColor(
					R.color.transparent));
		}
		return convertView;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (context.getClass().equals(LoginActivity.class)) {
			convertView = initLoginMenu(position, convertView, parent);
		} else if (context.getClass().equals(SettingActivity.class)) {
			if (type == TYPE_SETTINGMENU) {
				convertView = initSettingMenu(position, convertView, parent);
			} else if (type == TYPE_LOGINTYPE) {
				convertView = initLoginTypeMenu(position, convertView, parent);
			} else if (type == TYPE_LIST) {
				convertView = initSettingListView(position, convertView, parent);
			}
		}
		return convertView;
	}

}
